package com.easyweb.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.easyweb.cache.StartUpListener;
import com.easyweb.common.Constants;
import com.easyweb.common.PrimaryKeyGenerator;
import com.easyweb.common.PrintUtils;
import com.easyweb.model.Article;
import com.easyweb.model.ResultMsg;
import com.easyweb.model.Tag;
import com.easyweb.model.Users;
import com.easyweb.service.ArticleService;
import com.easyweb.service.TagService;

@Controller
@RequestMapping("/tag")
public class TagController {

	@Autowired
	private TagService tagService;
	
	@Autowired
	private ArticleService articleService;
	
	@RequestMapping(value="/merge",method=RequestMethod.POST)
	public void add(HttpServletRequest request,HttpServletResponse response){
		Users user = (Users) request.getSession().getAttribute("user");
		if(user == null){
			PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR, Constants.EXPIRE));
			return;
		}
		//id
		String id = request.getParameter("id");
		//名称
		String tagName = request.getParameter("tagName");
		if(StringUtils.isEmpty(tagName)){
			PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR, StartUpListener.LANGUAGE.getProperty("tagName.notnull")));
			return;
		}
		String tagColor = request.getParameter("tagColor");
		Tag tag = new Tag();
		if(StringUtils.isEmpty(id)){
			tag.setId(PrimaryKeyGenerator.generatePrimaryKey());
		}else{
			tag.setId(id);
		}
		tag.setTagName(tagName);
		tag.setTagColor(tagColor);
		int num = 0;
		if(StringUtils.isEmpty(id)){
			num = tagService.insertTag(tag);
		}else{
			num = tagService.updateTag(tag);
		}
		if(num > 0){
			StartUpListener.refreshTagCache(tagService, tag.getId());
			PrintUtils.printMsg(response, new ResultMsg());
		}else{
			PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR,null));
		}
	}
	
	@RequestMapping(value="/list",method=RequestMethod.GET)
	public void list(HttpServletRequest request,HttpServletResponse response){
		Users user = (Users) request.getSession().getAttribute("user");
		if(user == null){
			PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR, Constants.EXPIRE));
			return;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		String pageSize = request.getParameter("pageSize");
		String pageStart = request.getParameter("pageStart");
		if(!StringUtils.isEmpty(pageSize)){
			map.put("pageSize", Integer.parseInt(pageSize));
		}
		if(!StringUtils.isEmpty(pageStart)){
			map.put("start",  Integer.parseInt(pageSize)*(Integer.parseInt(pageStart)-1));
		}
		PrintUtils.printMsg(response, tagService.getTags(map));
	}
	
	@RequestMapping(value="/get",method=RequestMethod.GET)
	public void get(HttpServletRequest request,HttpServletResponse response){
		Users user = (Users) request.getSession().getAttribute("user");
		if(user == null){
			PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR, Constants.EXPIRE));
			return;
		}
		String id = request.getParameter("id");
		Map<String, Object> map = new HashMap<String, Object>();
		if(StringUtils.isEmpty(id)){
			PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR, StartUpListener.LANGUAGE.getProperty("id.notnull")));
			return;
		}
		map.put("id", id);
		PrintUtils.printMsg(response, tagService.getTagById(map));
	}
	
	@RequestMapping(value="/del",method=RequestMethod.GET)
	public void del(HttpServletRequest request,HttpServletResponse response){
		Users user = (Users) request.getSession().getAttribute("user");
		if(user == null){
			PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR, Constants.EXPIRE));
			return;
		}
		String id = request.getParameter("id");
		Map<String, Object> map = new HashMap<String, Object>();
		if(StringUtils.isEmpty(id)){
			PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR, StartUpListener.LANGUAGE.getProperty("id.notnull")));
			return;
		}
		map.put("tag", id);
		List<Article> articles = articleService.getArticles(map);
		if(articles==null||articles.size()==0){
			map.put("id", id);
			int num = tagService.deleteTagById(map);
			if(num > 0){
				StartUpListener.refreshTagCache(tagService, id);
				PrintUtils.printMsg(response, new ResultMsg());
			}else{
				PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR,null));
			}
		}else{
			PrintUtils.printMsg(response, new ResultMsg(Constants.STATUS_ERROR,StartUpListener.LANGUAGE.getProperty("tag.hasarticle")));
		}
	}
}
